<template>
    <div class="chatC">
        <div class="title">
            <div class="searchline">
                <img style="height: 80%; margin-top: 1%;" src="search.png" />
                <input @input="inputChange" v-model="userName" class="inputbox" type="text">
            </div>
            <div @click="showDefault" class="addbutton">
                <img  style="height: 80%" :src="isUserList? 'icon-adduser.png':'cancel.png'" />
            </div>
        </div>
        <div v-if="isUserList" class="chatlistcontainer">

            <div @click="showApplicationListContent"  class="chatlistitem" :style="-1 == currentSelect ? {backgroundColor:'beige'}:{}">
                <div class="itemcontainer">
                    好友申请
                </div>
            </div>

            <div v-for="item,key in props.friendList"  class="chatlistitem">
                <div @click="showUserInfo(key)"  class="itemcontainer" :style="key == currentSelect ? {backgroundColor:'beige'}:{}">
                    <div class="userprofile">头像</div>
                    <div class="usermsg">
                            <div style="font-size: 20px;line-height: 50px;">{{ item.friendName }}</div>
                    </div>
                </div>
            </div>
        </div>

        <div v-if="!isUserList" class="chatlistcontainer">
            <div @click="showNewFriendInfo(key)" v-for="item,key in newFriendList"  class="chatlistitem">
                <div class="itemcontainer" :style="key == currentSelect ? {backgroundColor:'beige'}:{}">
                    <div class="userprofile">头像</div>
                    <div class="usermsg">
                        <div class="nameandtime">
                            <div style="font-size: 20px;">{{ item.userName }}</div>
                        </div>
                        <div style="color: rgb(190, 190, 190); margin-top: 5px;">something about user</div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</template>
<script setup lang="ts">
import {ref, onMounted} from 'vue'
import chatUserApi from '@/api/chatUser/index'
import type { Relationship } from '@/types';
onMounted(() => {
    console.log(" i am here");
})
const emit = defineEmits<{
    (ev:'chatUserChange',userName:string):void
    (ev:'modelChange',current:number):void
    (ev:'showNewFriendInfo',userName:string):void
    (ev:'showUserInfo',relationship:Relationship):void
    (ev:'showApplicationListContent'):void
    (ev:'showDefault'):void
}>()
const props = defineProps<{
    friendList:Array<Relationship>
}>()
const isUserList = ref(true)
const currentSelect = ref<number>()
const userName = ref('')
const newFriendList = ref<Array<any>>()

const showApplicationListContent = () => {
    currentSelect.value = -1
    emit('showApplicationListContent')
}

const showUserInfo = (current:number) => {
    currentSelect.value = current
    emit('showUserInfo',props.friendList[current])
}
const showDefault = () => {
    isUserList.value = !isUserList.value
    userName.value = ''
    if(isUserList.value) {
        const current = currentSelect.value as number
        console.log(current);
        if(!current) {
            emit('showDefault')
        }else {
            if(current == -1) {
                emit('showApplicationListContent')
            }else{
                emit('showUserInfo',props.friendList[current])
            }
        }
    }else{
        emit('showDefault')
    }
   
}
const inputChange = () => {
    if(isUserList.value) {

    }else{
        console.log('e');
        chatUserApi.listByName(userName.value).then(res => {
            newFriendList.value = res.data.list;
        })
    }
}
const showNewFriendInfo = (index:number) => {
    const list =  newFriendList.value as Array<any>
    emit('showNewFriendInfo',list[index].userName)
}
</script>
<style scoped>
.searchline {
    height: 33px;
    width: 238px;
    background-color: rgb(226, 226, 226);
    border-radius: 8px;
    margin-left: 18px;
    margin-top: 30px;
    display: flex;
    flex-direction: row;
    overflow: hidden;
}
.inputbox {
    outline:none;
    height: 31px;
    width: 238px;
    font-size: 15px;
    line-height: 33px; 
    border: 0px;
    border-radius: 0 8px 8px 0;
    background-color: rgb(226, 226, 226);
}
.addbutton {
    height: 33px;
    width: 33px;
    background-color: rgb(226, 226, 226);
    border-radius: 8px;
    margin-top: 30px;
    margin-left: 10px;
    cursor: pointer;
    font-size: 33px;
    line-height: 33px;
    text-align: center;
}
.addbutton:hover{
    background-color: rgb(215, 199, 179);
}
.chatC {
    width: 313px;
    height: 100%;
    overflow: hidden;
    display: flex;
    flex-direction: column;
}
.chatlistcontainer{
    flex: 1;
    display: flex;
    flex-direction: column;
    overflow: auto;
}
.title {
    height: 80px;
    background-color: rgb(243, 243, 243);
    display: flex;
    flex-direction: row;
}
.chatlistitem{
    min-height: 80px;
    transition: all ease 0.3s;
    /* padding: 10px; */
}
.chatlistitem:hover{
    background-color: beige;
}
.itemcontainer {
    height: 100%;
    display: flex;
    flex-direction: row;
    /* justify-content: center; */
    align-items: center;
    cursor: pointer;
}
.userprofile{
    height: 50px;
    width: 50px;
    background-color: black;
    margin-left: 20px;
}
.usermsg {
    height: 50px;
    width: 210px;
    margin-left: 5px;
    margin-right: 10px;
}
.nameandtime{
    display: flex;
    flex-direction: row;
}
</style>